package cn.gzy.service.impl;

import cn.gzy.dto.LoginUser;
import cn.gzy.mapper.UserMapper;
import cn.gzy.pojo.User;
import cn.gzy.service.UserService;
import cn.gzy.util.JWTUtil;
import cn.gzy.util.PageUtil;
import cn.gzy.vo.Follow;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public String login(LoginUser loginUser) {
        try {
            // 从数据库中检索用户
            User user = userMapper.searchByAccountAndPassword(loginUser.getAccount(), loginUser.getPassword());
            if (user == null) {
                return null; // 用户不存在或密码错误
            }

            // 生成JWT令牌
            String token = JWTUtil.createToken(user.getUserId());
            return token;
        } catch (Exception e) {
            e.printStackTrace();
            return null; // 登录失败
        }
    }

    @Override
    public Integer updateUserPassword(User user) {
        User u = new User();
        u.setAccount(user.getAccount());
        return userMapper.updateByCondition(user,u);
    }


    @Override
    public User findByUserId(Integer id) {
        return userMapper.searchById(id);
    }

    @Override
    public PageUtil<Follow> findFollowers(Integer id, Integer current, Integer size, String key) {
        return findFollow(id, 1, current, size, key); // 1 表示关注者类型
    }

    @Override
    public PageUtil<Follow> findFollowed(Integer id, Integer current, Integer size, String key) {
        return findFollow(id, 0, current, size, key); // 0 表示被关注者类型
    }

    private PageUtil<Follow> findFollow(Integer id, Integer type, Integer current, Integer size, String key) {
        Integer totalCount = userMapper.countOfFollow(id, type, key);
        PageUtil<Follow> followPage = new PageUtil<>(current, size, totalCount);
        List<Follow> follows = userMapper.followship(id, type, (current - 1) * size, size, key);
        followPage.setList(follows);
        return followPage;
    }

    @Override
    public Integer updateUser(User user) {
        User condition = new User();
        condition.setUserId(user.getUserId());
        // 注意：这里假设密码字段不需要加密更新，如果是，应在此处加密
        return userMapper.updateByCondition(user, condition);
    }

    @Override
    public Integer delAttention(Integer from, Integer to) {
        return userMapper.delAttention(from, to);
    }

    @Override
    public Integer register(User user) {
        try {
            // 在实际应用中，密码应加密存储
            user.setPassword(user.getPassword()); // 假设直接存储明文密码，实际应用中应加密

            // 注册用户
            userMapper.registerUser(user);

            return 1; // 操作成功
        } catch (Exception e) {
            e.printStackTrace();
            return 0; // 操作失败
        }
    }
    @Override
    public Integer addAttention(Integer from, Integer to) {
        return userMapper.addAttention(from, to);
    }
    @Override
    public boolean isUsernameExists(String username) {
        return userMapper.isUsernameExists(username) > 0;
    }

    @Override
    public boolean isEmailExists(String email) {
        return userMapper.isEmailExists(email) > 0;
    }

    @Override
    public User isadmin(Integer userId) {
        return userMapper.isadmin(userId);
    }
}